package com.jingzhi.huimiao;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jingzhi.huimiao.bean.DramaInfo;
import com.jingzhi.huimiao.utils.DataStoreUtil;
import com.jingzhi.huimiao.utils.SQLdm;

/**
 * Task 界面
 * 
 * @author cxk
 * 
 * 
 */
public class TaskActivity extends Activity implements OnClickListener {

	private ImageView task1;
	private ImageView task2;
	private ImageView task3;
	private ImageView task4;
	private TextView tv_title;
	private TextView study;
	private TextView emigrated;
	private LinearLayout iv_back_main;
	private SQLiteDatabase db;
	private ArrayList<Long> tasklist;
	private Intent tintent;

	private int[] mImageResourceIds;
	private Gallery gallery;

	private int selectNum;
	private MyAdapter adapter;
	private long taskid;
	private Long savedrama;
	private int[] ids;
	private ArrayList<DramaInfo> dramainfos;
	private RelativeLayout rl_drama;
	private RelativeLayout rl_task;
	private ImageView iv_drama_bg;
	private TextView tv_drama_drama;

	private int i;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_task);

		// 重新设置chooseTask为0
		DataStoreUtil.putint(TaskActivity.this, "CHOOSETASK", (long) 0);

		Long chaptercount = DataStoreUtil.getLong(TaskActivity.this,
				"CHOOSRCHAPTER");
		Log.e("chapterid", chaptercount + "");

		initview();

		// 打开数据库输出流
		SQLdm s = new SQLdm();
		db = s.openDatabase(getApplicationContext());
		
		// 判断是否显示剧情
				savedrama = DataStoreUtil.getLong(TaskActivity.this, "SHOWDRAMA");
				if (savedrama < chaptercount) {// 未打开过，需显示剧情
					ids = new int[] {R.drawable.drama_bg0, R.drawable.drama_bg1,
							R.drawable.drama_bg2, R.drawable.drama_bg3,
							R.drawable.drama_bg4, R.drawable.drama_bg5,
							R.drawable.drama_bg6, R.drawable.drama_bg7,
							R.drawable.drama_bg8, R.drawable.drama_bg9 
					};
					// 显示剧情
					showDrama();
					// 存储新值
					DataStoreUtil.putint(TaskActivity.this, "SHOWDRAMA", chaptercount);
				}
		

		// 查询数据库中testid=1的数据

		tasklist = new ArrayList<Long>();

		// 查询小关中的task 返回值 是 task集合
		String sql = "select * from unit where pid=?;";
		Cursor cursor = db.rawQuery(sql,
				new String[] { chaptercount.toString() });
		while (cursor.moveToNext()) {
			long taskid = cursor.getLong(cursor.getColumnIndex("id"));
			Log.e("taskid----", taskid + "");
			tasklist.add(taskid);
		}

		adapter = new MyAdapter();

		gallery = (Gallery) findViewById(R.id.gallery1);
		// gallery.onScroll(e1, e2, distanceX, distanceY)

		gallery.setOnItemSelectedListener(new Gallery.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				selectNum = position;//
				adapter.notifyDataSetChanged();

				// Toast.makeText(TaskActivity.this, "已选择TASK"+(position+1),
				// 0).show();
				DataStoreUtil.putint(TaskActivity.this, "CHOOSETASK",
						tasklist.get(position));

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}

		});
		gallery.setAdapter(adapter);

		if (cursor != null) {
			cursor.close();
		}
		if (db != null) {
			db.close();
		}

	}

	/**
	 * 显示剧情
	 */
	private void showDrama() {
		// TODO Auto-generated method stub
		// 根据所选章节查询对应的对话
		Cursor dramacur = db.rawQuery(
				"select * from drama where chapter_id = 8", null);
		dramainfos = new ArrayList<DramaInfo>();
		while (dramacur.moveToNext()) {
			DramaInfo dramainfo = new DramaInfo();
			dramainfo.content = dramacur.getString(dramacur
					.getColumnIndex("content"));
			dramainfo.pic_number = dramacur.getInt(dramacur
					.getColumnIndex("pic_number"));
			dramainfos.add(dramainfo);
		}
		if(dramacur!=null){
			dramacur.close();
		}
		rl_drama.setVisibility(View.VISIBLE);
		rl_task.setEnabled(false);
		changeDrama(i);
		rl_drama.setOnClickListener(this);
	}

	/**
	 * 改变剧情图片语句
	 * @param arg
	 */
	private void changeDrama(int arg) {
		if(arg<dramainfos.size()){
			iv_drama_bg.setBackgroundResource(ids[dramainfos.get(arg).pic_number]);
			tv_drama_drama.setText(dramainfos.get(arg).content);
		}else{
			rl_drama.setVisibility(View.GONE);
		}
		
	}

	private void initview() {
		task1 = (ImageView) findViewById(R.id.iv_task1);
		task2 = (ImageView) findViewById(R.id.iv_task2);
		task3 = (ImageView) findViewById(R.id.iv_task3);
		task4 = (ImageView) findViewById(R.id.iv_task4);
		study = (TextView) findViewById(R.id.btn_study);
		tv_title = (TextView) findViewById(R.id.tv_title);
		emigrated = (TextView) findViewById(R.id.btn_emigrated);
		iv_back_main = (LinearLayout) findViewById(R.id.iv_back_tomain);

		rl_drama = (RelativeLayout) findViewById(R.id.rl_drama);
		rl_task = (RelativeLayout) findViewById(R.id.rl_task);
		iv_drama_bg = (ImageView) findViewById(R.id.iv_drama_bg);
		tv_drama_drama = (TextView) findViewById(R.id.tv_drama_drama);
		
		// iv_gallery = (ImageView) findViewById(R.id.iv_gallery);

		addListener();
	}

	private void addListener() {
		study.setOnClickListener(this);
		emigrated.setOnClickListener(this);
		iv_back_main.setOnClickListener(this);
		task1.setOnClickListener(this);
		task2.setOnClickListener(this);
		task3.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_study:
			taskid = DataStoreUtil.getLong(TaskActivity.this, "CHOOSETASK");
			// Log.e("Taskid-----renturn", taskid+"");
			if (selectNum + 1 == 4) {
				Toast.makeText(TaskActivity.this, "task4不能学习哟", 0).show();
				// startActivity(new Intent(TaskActivity.this,
				// Test4Activity.class));
				// finish();
			} else if (taskid == 0) {
				Toast.makeText(TaskActivity.this, "请选择TASK", 0).show();
			} else {
				startActivity(new Intent(TaskActivity.this, StudyActivity.class));
				finish();
			}

			break;
		case R.id.btn_emigrated:
			taskid = DataStoreUtil.getLong(TaskActivity.this, "CHOOSETASK");
			// Log.e("Taskid-----renturn", taskid+"");
			if (selectNum + 1 == 4) {
				startActivity(new Intent(TaskActivity.this, Test4Activity.class));
				finish();
			} else if (taskid == 0) {
				Toast.makeText(TaskActivity.this, "请选择TASK", 0).show();
			} else {
				startActivity(new Intent(TaskActivity.this, TestActivity.class));
				finish();
			}

			break;
		case R.id.iv_back_tomain:
			startActivity(new Intent(TaskActivity.this, MainActivity.class));
			finish();
			break;

		case R.id.rl_drama:
			i++;
			changeDrama(i);
			break;
		default:
			break;
		}

	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// getMenuInflater().inflate(R.menu.activity_main, menu);
	// return true;
	// }

	class MyAdapter extends BaseAdapter {

		int[] images = { R.drawable.task_1, R.drawable.task_2,
				R.drawable.task_3, R.drawable.task_4 };

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO 自动生成的方法存根
			// ImageView imageView=new ImageView(TaskActivity.this);
			// imageView.setImageResource(images[position]);
			// imageView.setPadding(80, 0, 80, 40);
			// imageView.setBackgroundColor(Color.rgb(235, 235, 235));
			// System.out.println("调用了getView,当前Position为"+position);

			final ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = View.inflate(TaskActivity.this,
						R.layout.task_item, null);
				holder.image = (ImageView) convertView
						.findViewById(R.id.taskimage);
				// holder.imageRel =
				// (RelativeLayout)convertView.findViewById(R.id.image_rel);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.image.setImageResource(images[position]);

			if (selectNum == position) {
				holder.image.setLayoutParams(new LinearLayout.LayoutParams(784,
						964));// 如果被选择则放大显示
				// holder.image.setAnimation(animation);
				holder.image.setAlpha(250);
			} else {
				holder.image.setLayoutParams(new LinearLayout.LayoutParams(584,
						664));// 否则正常
				holder.image.setAlpha(100);
			}
			return convertView;
		}

		public long getItemId(int position) {
			// TODO 自动生成的方法存根
			System.out.println("调用了getItemId,当前Position为" + position);
			return position;
		}

		public Object getItem(int position) {
			// TODO 自动生成的方法存根
			System.out.println("调用了getItem,当前Position为" + position);
			return position;
		}

		public int getCount() {
			// TODO 自动生成的方法存根
			System.out.println("调用了getCount");
			return images.length;
		}

		class ViewHolder {
			ImageView image;
			// RelativeLayout imageRel;
		}
	}
}
